(function($) {

	$.jgrid.extend( {
				setComplexGroupHeaders : function(o) {
					o = $.extend( {
						useColSpanStyle : false,
						groupHeaders : []
					}, o || {});

					return this.each(function() {
						if (!o) {
							return;
						}
						// 把数据存起来
							var headers = this.p.complexHeader || [];
							headers.push(o);
							this.p.complexHeader = headers;

							var ts = this;
							var numberOfColumns, titleText, colModel = ts.p.colModel, htable = $(
									'table.ui-jqgrid-htable', ts.grid.hDiv), // 获取表头的table
							thead = htable.children('thead'), firstHeaderRow = htable
									.find('.jqg-first-row-header'), tr = $(
									thead).children('tr'), gridId = this.id;

							if (!tr) {
								return;
							}

							var newTr = $(firstHeaderRow).clone();
							newTr.removeClass();
							newTr
									.addClass('ui-jqgrid-labels jqg-' + tr.length + '-row-header');
							newTr.attr('role', 'rowheader');
							newTr.attr('aria-hidden', 'false');
							$(firstHeaderRow).after(newTr);// 复制第一行tr，并且插入到第一行后
							var groupHeaders = o.groupHeaders, nextTr = $(newTr)
									.next();

							var newThs = $(newTr).children();
							var nextTrThs = $(nextTr).children();
							// 先根据groupHeaders设置表头
							for ( var i = 0; i < newThs.length; i++) {
								var th = $(newThs[i]);
								var column = colModel[i];
								var code = column.name;
								// 设置class和相关的属性
								th.addClass('ui-state-default ui-th-column ui-th-ltr');
								th.attr('role', 'columnheader');
								th.removeAttr('style');
								th.css('border-bottom', '1px solid #0559C4');

								if (column.hidden) {// 隐藏列
									th.css('display', 'none');
								}

								var header = getHeader(column, groupHeaders);
								if (header) {
									th.append(header.titleText);
									th.attr('colspan', header.numberOfColumns);
									if (header.cls) {
										th.removeClass();
										th.addClass(header.cls);
									}

								} else {
									th.attr('cell-name', gridId + '_' + code);
									th.attr('custom-cell', true);
								}
							}

							// 删除单元格
							for ( var i = 0; i < newThs.length; i++) {
								var rmCols = 0;
								var th = $(newThs[i]);
								var colspan = parseInt(th.attr('colspan') || 1);
								rmCols += colspan - 1;
								while (rmCols > 0) {
									$(newThs[rmCols + i]).remove();
									rmCols--;
								}
							}

							var firstCel = 0;
							for ( var i = 0; i < newThs.length; i++) {
								var th = $(newThs[i]);
								if ('true' == th.attr('custom-cell')) {
									var prev = null;
									if (i - 1 >= 0) {
										prev = $(newThs[i - 1])
									}
									headerGroup(th, prev, firstCel, nextTrThs);
								}
								firstCel += colspan;
							}

							$(newTr).children('th[higher-idx]').each(
											function() {
												if ('true' == $(this).attr(
														'delete')) {
													$(this).remove();
												} else {
													var higherColspan = parseInt($(
															this).attr(
															'higher-colspan') || 1);
													var counter = parseInt($(
															this).attr(
															'counter') || 1);
													$(this).attr('colspan',
															counter);
													if (higherColspan == counter) {
														$(this).attr(
																'need-rowspan',
																'true');
													}
												}
											});

							if (o.useColSpanStyle) {
								var c = 0;
								$(newTr).children('th[need-rowspan="true"]').each(
												function() {
													var higherIdx = $(this)
															.attr('higher-idx');
													if (higherIdx) {
														higherIdx = parseInt(higherIdx)
																- c;
														var childs = $(nextTr)
																.children();
														var rowspanTh = $(childs[higherIdx]);
														var rowNum = $(this)
																.attr('row-num');
														$(this)
																.after(
																		rowspanTh);
														$(rowspanTh).attr(
																'rowspan',
																rowNum);
														$(this).remove();
														c++;
													}
												});
							}

						});
				}
			});

	function getHeader(column, groupHeaders) {
		var name = column.name;
		for ( var i in groupHeaders) {
			var header = groupHeaders[i];
			if (name === header.startColumnName) {
				return header;
			}
		}
		return null;
	}
	;

	function headerGroup(th, prev, firstCel, nextTrThs) {// 根据上一级表头设置colspan和rowspan
		var lastCel = firstCel + parseInt($(th).attr('colspan') || 1);
		var nextFirstCel = 0;
		for ( var i = 0; i < nextTrThs.length; i++) {
			var nextTh = nextTrThs[i];
			var colspan = parseInt($(nextTh).attr('colspan') || 1);
			var nextLastCel = nextFirstCel + colspan;
			if (firstCel >= nextFirstCel && lastCel <= nextLastCel) {
				var rowspan = parseInt($(nextTh).attr('rowspan') || 1);
				$(th).attr('higher-idx', i);
				$(th).attr('row-num', rowspan + 1);
				$(th).attr('higher-colspan', colspan);
				var counter = parseInt($(prev).attr('counter') || 1);
				if (i == $(prev).attr('higher-idx')) {
					$(prev).attr('delete', true);
					$(th).attr('counter', counter + 1);
				}
				break;
			}
			nextFirstCel += colspan;
		}
	}
	;

})(jQuery);